<template>
  <div id="detail">
    <!-- 导航 -->
    <detail-nav-bar ref="nav" class="detail-nav" @titleClick="titleClick"/>
    <scroll class="content" ref="scroll" @scroll="contentScroll" :probe-type="3">
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="this.goods"/>
      <detail-shop-info :shop="this.shop"/>
      <detail-goods-info :detail-info="this.detailInfo" @imageLoad="imageLoad"/>
      <detail-param-info ref="param" id="param" :param-info="this.paramInfo"/>
      <detail-comment-info ref="comment" id="comment" :comment-info="this.commentInfo"/>
      <goods-list ref="goodsY" id="goodsY" :goods="this.recommends"/>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"/>
    <back-top class="back" @click="backClick" v-show="isShow"/>
    <toast ref="toast"/>
  </div>
</template>

<script>
import DetailNavBar from "@/views/detail/childComps/DetailNavBar";
import DetailSwiper from "@/views/detail/childComps/DetailSwiper";
import DetailBaseInfo from "@/views/detail/childComps/DetailBaseInfo";
import DetailShopInfo from "@/views/detail/childComps/DetailShopInfo";
import DetailGoodsInfo from "@/views/detail/childComps/DetailGoodsInfo";
import DetailParamInfo from "@/views/detail/childComps/DetailParamInfo";
import DetailCommentInfo from "@/views/detail/childComps/DetailCommentInfo";
import DetailBottomBar from "@/views/detail/childComps/DetailBottomBar";

import Scroll from "@/components/common/scroll/Scroll";
import GoodsList from "@/components/content/goods/GoodsList";
import Toast from '@/components/common/toast/Toast'

import {getDetail, Goods, Shop, GoodsParam, getRecommend} from "@/network/detail";
import {debounce} from "@/common/utils";
import {backTop} from "@/common/mixin";
import {mapActions} from 'vuex'


export default {
  name: "Detail",
  components: {
    Scroll,
    DetailNavBar,
    DetailSwiper,
    DetailShopInfo,
    DetailBaseInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    Toast
  },
  mixins: [backTop],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      savesY: [],
      imageLoadBool: false,
      currentIndex: 0,
      ins: null
    }
  },
  created() {
    //保存传入的iis
    this.iid = this.$route.params.iid

    //请求详情数据
    getDetail(this.iid).then(res => {

      const shuJ = res.data.result

      /* 轮播图数据 */
      this.topImages = shuJ.itemInfo.topImages

      /* 获取详情页信息 */
      this.goods = new Goods(shuJ.itemInfo, shuJ.columns, shuJ.shopInfo.services)

      /* 获取店家信息 */
      this.shop = new Shop(shuJ.shopInfo)

      /* 获取服装详细信息 */
      this.detailInfo = shuJ.detailInfo

      /* 获取衣服参数信息 */
      this.paramInfo = new GoodsParam(shuJ.itemParams.info, shuJ.itemParams.rule)

      /*  获取评论信息 */
      if (shuJ.rate.cRate !== 0) {
        this.commentInfo = shuJ.rate.list[0]
      }
    })

    //请求推荐数据
    getRecommend().then(res => {
      this.recommends = res.data.data.list
    })
  },
  methods: {
    ...mapActions(['addCart']),
    imageLoad() {
      this.imageLoadBool = true
      this.savesY.push(0)
      this.savesY.push(-this.$refs.param.$el.offsetTop + 45)
      this.savesY.push(-this.$refs.comment.$el.offsetTop + 45)
      this.savesY.push(-this.$refs.goodsY.$el.offsetTop + 45)
      this.savesY.push(-Number.MAX_VALUE)
    },
    titleClick(index) {
      // this.$refs.scroll.scroll.scrollTo(0, -this.savesY[index], 250)
      if (this.imageLoadBool) {
        switch (index) {
          case 0 :
            this.$refs.scroll.scroll.scrollTo(0, 0, 200)
            break
          case 1:
            this.$refs.scroll.scroll.scrollToElement("#param", 200)
            break
          case 2:
            this.$refs.scroll.scroll.scrollToElement("#comment", 200)
            break
          case 3:
            this.$refs.scroll.scroll.scrollToElement("#goodsY", 200)
            break
        }
      } else {
        this.$refs.toast.show('请等待图片加载完成', 2000)
      }
    },
    contentScroll(pro) {
      this.isShow = pro.y < -1000
      const proY = pro.y
      /* 对比 */
      for (let i = 0; i < this.savesY.length - 1; i++) {
        if (this.currentIndex !== i &&
          (proY <= this.savesY[i] && proY > this.savesY[i + 1])) {
          this.currentIndex = i
          this.$refs.nav.currentIndex = this.currentIndex
        }
      }
    },
    addToCart() {
      /* 1.获取购物车需要展示的信息 */
      const product = {}
      /* 图片 */
      product.image = this.topImages[0];
      /* 介绍 */
      product.title = this.goods.title;
      /* 商品介绍 */
      product.desc = this.goods.desc;
      /* 真实价格 */
      product.price = this.goods.realPrice;
      /* id */
      product.iid = this.iid

      /* 2.将商品添加到购物车 */
      // this.$store.commit('addCart', product)
      // this.$store.dispatch('addCart', product).then(res => {
      //   /* 商品添加成功 */
      //   console.log(res)
      // })
      /* 通过mapActions映射，直接调用 */
      this.addCart(product).then(res => {
        this.$refs.toast.show(res, 2000)
      })
    },
  },
}
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #ffffff;
  height: 100vh;
}

.content {
  height: calc(100% - 44px - 49px);
  overflow: hidden;
}

.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #ffffff;
}

.back {
  margin-bottom: 10px;
}


</style>
